from bs4 import BeautifulSoup


text_str = """<html>
	<head>
		<title>测试bs4模块脚本</title>
	</head>
	<body>
		<h2>橡皮擦的爬虫课</h2>
		<p>用1段自定义的 HTML 代码来演示</p>
		<p>用2段自定义的 HTML 代码来演示</p>
	</body>
</html>
"""
# 实例化 Beautiful Soup 对象
soup = BeautifulSoup(text_str, "html.parser")
# 上述是将字符串格式化为 Beautiful Soup 对象，你可以从一个文件进行格式化
# soup = BeautifulSoup(open('test.html'))
print(soup)
# 输入网页标题 title 标签
print(soup.title)
# 输入网页 head 标签
print(soup.head)

# 测试输入段落标签 p
print(soup.p) # 默认获取第一个

'''
html = """<html><head><title>The Dormouse's story</title></head>
<body>
<p class="title"><b>The Dormouse's story</b></p>
<p class="story">Once upon a time there were three little sisters; and their names were
<a href="http://example.com/elsie" class="sister" id="link1">Elsie</a>,
<a href="http://example.com/lacie" class="sister" id="link2">Lacie</a> and
<a href="http://example.com/tillie" class="sister" id="link3">Tillie</a>;
and they lived at the bottom of a well.</p>
<p class="story">...</p>
"""
soup = BeautifulSoup(html,'lxml')#features:'lxml'
# print(type(soup),soup)
# a = soup.prettify()#漂亮结构化的打印
a = soup.title.name#标签名称
a= soup.title.string#标签的内容
# a = soup.find_all('a')#找到所有的标签
print(a)
# for i in a:
    # print(i.get('href'))#找到href属性下的链接地址
'''